

*--------------------------------------------------------------*
*             Iraq Conflict Study Replication Code - STATA 17  *
*--------------------------------------------------------------*
clear
* 2. Import data
use "Iraq.dta", clear


*---------------------------------------------------------------------*
* 1. Data Filtering: Restrict to Iraq & reliable Government observations
*---------------------------------------------------------------------*

keep if iso3c           == "IRQ"
keep if side_a          == "Government of Iraq"
keep if event_clarity==1 & year > 2003

*---------------------------------------------------------------------*
* 2. Encode Fixed Effects
*---------------------------------------------------------------------*

encode adm_1, generate(ID_name1)
encode adm_2, generate(ID_name2)
encode side_b, generate(ID_side_b)

*---------------------------------------------------------------------*
* 3. Main Dependent Variables (DV)
*---------------------------------------------------------------------*

* GTPSV: Government Territorial Power Share Variation
generate GTME           = deaths_b / (deaths_a + deaths_b)
replace  GTME           = .      if (deaths_a + deaths_b)==0
label   var GTME        "GTME"

* LER: Lethality Ratio
generate LER             = (1 + deaths_b) / (1 + deaths_a)
replace  LER             = .      if (deaths_a + deaths_b)==0
label   var LER          "LER"

* lnLER_gov: Natural log of LER
generate lnLER_gov       = ln(LER)
label   var lnLER_gov    "LER (ln)"

* GTVic: Government Tactical Victory Indicator
generate GTVic           = .
replace  GTVic           = 0      if deaths_a > deaths_b
replace  GTVic           = 1      if deaths_a < deaths_b
replace  GTVic           = .      if (deaths_a + deaths_b)==0
label   var GTVic        "GTVic"

*---------------------------------------------------------------------*
* 4. Main Independent Variables (INDIV)
*---------------------------------------------------------------------*

* Distance to Baghdad
label   var dist_baghdad "Distance"

* Aid (logged)
generate lnAid           = .
label   var lnAid        "Aid (ln)"

* Interaction: lnAid * Distance
generate lnAid_dibXDist  = .
label   var lnAid_dibXDist "Aid (ln)*Distance"

*---------------------------------------------------------------------*
* 5. Control Variables
*---------------------------------------------------------------------*

* 5.1 Structural (Ethnic Groups)
generate ethnic          = 0
replace  ethnic          = 1      if group=="Sunni Arabs"
replace  ethnic          = 2      if group=="Shi'a Arabs"
replace  ethnic          = 3      if group=="Kurds"
replace  ethnic          = 4      if group=="Turkmen"

generate Sunni           = (ethnic==1)
label   var Sunni        "Sunni"

generate Shia            = (ethnic==2)
label   var Shia         "Shia"

generate Kurds           = (ethnic==3)
label   var Kurds        "Kurds"

generate Turkmen         = (ethnic==4)
label   var Turkmen      "Turkmen"

* 5.2 Population (lagged)
label   var pop_lag_1    "Pop."

* 5.3 Military personnel (logged)
generate ln_milper       = ln(1 + milper_lag_1)
replace  ln_milper       = .      if milper_lag_1 == -9
label   var ln_milper    "Military personnel"

* 5.4 Military support
generate mil_support     = ext_count_lag_1
label   var mil_support  "Mil. supp."

* 5.5 Situational controls: battles & actors
generate battles         = tot_btls_adm2_av_4_lag_1
label   var battles      "Battles"

generate actors          = tot_rbls_av_4_lag_1
label   var actors       "Actors"

* Other operational controls
label   var border_control "Border control"
label   var bw_off         "Breaking the walls"
label   var sh_off         "Soldier's Harvest"
label   var us_out         "US withdrawal"
label   var operation_sc   "Steel curtain"
label   var sayeed_off     "Sayeed"

* Terrain
label   var elevation      "Elevation"
label   var dist_oil       "Oil"

* Logistics index, adjusting for Mosul control
generate logi_SX         = logi_supp
replace  logi_SX         = .         if mosul_control==1
replace  logi_SX         = logi_supp2 if mosul_control==1
label   var logi_SX       "City"

*---------------------------------------------------------------------*
* 6. Time Dependencies
*---------------------------------------------------------------------*

* Weeks since Surge (set negatives to zero)
replace wks_snc_surge    = 0           if wks_snc_surge <= 0
label   var wks_snc_surge "Weeks since Surge"
generate wks_snc_surge_sq = wks_snc_surge^2
label   var wks_snc_surge_sq "Weeks since Surge (sq)"
generate wks_snc_surge_cu = wks_snc_surge^3
label   var wks_snc_surge_cu "Weeks since Surge (cu)"

* Weeks since Mosul fall
replace wks_since_mosulf_fall    = 0           if wks_since_mosulf_fall <= 0
label   var wks_since_mosulf_fall "Weeks since Mosul fall"
generate wks_since_mosulf_fall_sq = wks_since_mosulf_fall^2
label   var wks_since_mosulf_fall_sq "Weeks Mosul fall (sq)"
generate wks_since_mosulf_fall_cu = wks_since_mosulf_fall^3
label   var wks_since_mosulf_fall_cu "Weeks Mosul fall (cu)"

*---------------------------------------------------------------------*
* 7. Instruments
*---------------------------------------------------------------------*

label   var food_av_4_lag_6 "Food prices"
label   var diff_02_04      "Night light"

generate food_av_4_lag_6Xdist = food_av_4_lag_6 * dist_baghdad
generate diff_02_04Xdist      = diff_02_04 * dist_baghdad
label   var food_av_4_lag_6Xdist "Food prices*Distance"
label   var diff_02_04Xdist     "Night light*Distance"

*---------------------------------------------------------------------*
* 8. Global Macros for Controls & Model Setup
*---------------------------------------------------------------------*

global Essential    logi_SX
global Operational  bw_off sh_off sayeed_off
global Situational  battles elevation
global Strategic    border_control mil_support
global Ethnic       Sunni Shia Kurds Turkmen
global Main_controls $Essential $Situational $Operational $Strategic $Ethnic

global Time_depS    wks_snc_surge wks_snc_surge_sq wks_snc_surge_cu
global Time_depM    wks_since_mosulf_fall wks_since_mosulf_fall_sq wks_since_mosulf_fall_cu
global Additional   us_out actors pop_lag_1 ln_milper dist_oil $Time_depS $Time_depM
global FE           i.ID_side_b

* Update Aid variables now that total_disb_ehw_lag_1 is defined
replace lnAid          = ln(1 + (total_disb_ehw_lag_1/100000))
replace lnAid_dibXDist = dist_baghdad * lnAid

global DVln       lnLER
global DiVar      GTVic
global DV2        GTME
global IndepAidln lnAid lnAid_dibXDist
global IndepDist  dist_baghdad
global IVS        food_av_4_lag_6 diff_02_04 food_av_4_lag_6Xdist diff_02_04Xdist
global IV_setup   ($IndepAidln = $IVS)

global Conditions  where_prec <= 1 & date_prec <= 1
global Options_ln  cluster(ID_name2) endog(lnAid lnAid_dibXDist) partial(i.ID_side_b)
global Options     cluster(ID_name2)



*---------------------------------------------------------------------*
*                     Table Generation - pt1                   
*---------------------------------------------------------------------*

* Table 1A: IV regression + summary stats
ivreg2 $DV2 $IndepDist $Main_controls $FE $IV_setup if $Conditions, $Options_ln first
asdoc sum ///
    $DV2 $IndepDist $Essential $Situational $Operational $Strategic $Ethnic lnAid food_av_4_lag_6 diff_02_04 ///
    if e(sample), stat(N mean sd min max) replace label

* First‐stage regressions for Aid
ivreg2 $DV2 $IndepDist $Main_controls $FE $IV_setup if $Conditions, $Options_ln first

reg lnAid $IndepDist $IVS $Main_controls $FE if e(sample), cluster(ID_name2)
outreg2 using Tab2A.doc, ///
    ctitle("Model 1 (2SLS), 1st stage, Aid") ///
    sortvar($IndepDist $IVS $Main_controls $FE) ///
    keep($IndepDist $IVS $Main_controls $FE) ///
    addstat() tex dec(3) pdec(3) replace label

reg lnAid_dibXDist $IndepDist $IVS $Main_controls $FE if e(sample), cluster(ID_name2)
outreg2 using Tab2A.doc, ///
    ctitle("Model 1 (2SLS), 1st stage, Aid") ///
    sortvar($IndepDist $IVS $Main_controls $FE) ///
    keep($IndepDist $IVS $Main_controls $FE) ///
    addstat() tex dec(3) pdec(3) append label

*--------------------------------------------------------------*
*             Correlation Matrix & Heatplot (Fig. F1)          *
*--------------------------------------------------------------*
* 1. IV regression (first stage)
ivreg2  $DV2           ///
        $IndepDist     ///
        $Main_controls ///
        $FE            ///
        $IV_setup      ///
    if    $Conditions,   ///
        $Options_ln     ///
        first

* 2. Compute correlation matrix
correlate     $DV2         ///
              lnAid        ///
              $IndepDist   ///
              $Essential   ///
              $Situational ///
              $Operational ///
              $Strategic   ///
              $Ethnic      ///
    if e(sample)

matrix corrmatrix = r(C)

* 3. Plot heatmap of correlations
heatplot corrmatrix,               ///
    label                          ///
    xlab(, angle(45))              ///
    graphregion(fcolor(white))     ///
    colors(hcl diverging, gscale)  ///
    values(format(%4.2f))          ///
    legend(off)                    ///
    cuts(-1(`=2/15')1)

// Export figure
graph export "F1.tif", replace     width(6000)

*graph export "F1.jpg", replace width(5000)

*--------------------------------------------------------------*
*             Prepare Data for External Figures              
*--------------------------------------------------------------*
// Save sample datasets for R figures
tempfile df1 df2
save "Iraq_1.dta", replace
keep if e(sample)
save "Iraq_2.dta", replace
use "Iraq_1.dta",  replace

*---------------------------------------------------------------------*
*                    Run models for table 1                  
*---------------------------------------------------------------------*

* Main model (2SLS)
ivreg2  $DV2           $IndepDist      $Main_controls   $FE   $IV_setup   if $Conditions, $Options_ln  
estimates store Model_baseline2SLS

* Baseline model (OLS)
reg     $DV2           $IndepDist      $IndepAidln      $Main_controls   $FE   if $Conditions, $Options  
estimates store Model_baselineOLS

*---------------------------------------------------------------------*
*                    Run models for table 2                  
*---------------------------------------------------------------------*

* 2. Essential controls
ivreg2  $DV2           $IndepDist      $Essential               $FE       $IV_setup   if $Conditions, $Options_ln
estimates store Model_Essential

* 3. Situational controls
ivreg2  $DV2           $IndepDist      $Essential $Situational   $FE       $IV_setup   if $Conditions, $Options_ln
estimates store Model_Situational

* 4. Operational controls
ivreg2  $DV2           $IndepDist      $Essential $Operational     $FE       $IV_setup   if $Conditions, $Options_ln
estimates store Model_Operational

* 5. Strategic controls
ivreg2  $DV2           $IndepDist      $Essential $Strategic       $FE       $IV_setup   if $Conditions, $Options_ln
estimates store Model_Strategic

* 6. Ethnic controls
ivreg2  $DV2           $IndepDist      $Essential $Ethnic          $FE       $IV_setup   if $Conditions, $Options_ln
estimates store Model_Ethnic

* 7. Additional controls
ivreg2  $DV2           $IndepDist      $Main_controls $Additional  $FE       $IV_setup   if $Conditions, $Options_ln
estimates store Model_Additional

* 8. No‐outliers in Aid
ivreg2  $DV2           $IndepDist      $Main_controls           $FE       $IV_setup   if $Conditions & lnAid<2, $Options_ln
estimates store Model_NoOutAid

* 9. Probit (clustered SEs)
global Options   vce(cluster ID_name2)
ivprobit $DiVar   $IndepDist      $Essential $Situational $Operational $Strategic   $FE   $IV_setup   if $Conditions, $Options
estimates store Model_probit

* 10. Tobit (0–1 limits)
global Options   vce(cluster ID_name2) ll(0) ul(1)
ivtobit $DV2     $IndepDist      $Essential $Situational $Operational $Strategic   $FE   $IV_setup   if $Conditions, $Options
estimates store Model_Tobit

* 11. Log‐LER model
ivreg2  $DVln         $IndepDist      $Main_controls           $FE       $IV_setup   if $Conditions, $Options_ln
estimates store Model_Ln

* 12. Log‐LER (no outliers)
ivreg2  $DVln         $IndepDist      $Main_controls           $FE       $IV_setup   if $Conditions & LER<64 & LER>0.07, $Options_ln
estimates store Model_Ln_outl

* 13. Alternative aid variable
replace lnAid          = ln(1 + (total_disb_ehw_av_4_lag_1/100000))
replace lnAid_dibXDist = dist_baghdad * lnAid
global  IV_setup       ($IndepAidln = $IVS)  
global  Options_ln     cluster(ID_name2) endog(lnAid lnAid_dibXDist) partial(i.ID_side_b)

ivreg2  $DV2           $IndepDist      $Main_controls           $FE       $IV_setup   if $Conditions, $Options_ln
estimates store Model_alt_dv


*---------------------------------------------------------------------*
*                    Table 1                  
*---------------------------------------------------------------------*
** set global stats ols
global Add_stat_OLS			                                 ///
Hansen J statistic,  							-99999,		///
J statistic degrees of freedom, 				-99999,		///
p-value of Hansen J statistic, 					-99999,		///
GMM distance test statistic of endogeneity,   	-99999, 	///
p-value of GMM distance test statistic, 		-99999,	///
Number of ID,									e(N_clust)

** set global stats 2sls
global Add_stat			                                   	///
Hansen J statistic,  							e(j),		///
J statistic degrees of freedom, 				e(jdf),		///
p-value of Hansen J statistic, 					e(jp),		///
GMM distance test statistic of endogeneity,   	e(estat), 	///
p-value of GMM distance test statistic, 		e(estatp),	///
Number of ID,									e(N_clust)

** OLS
estimates restore  Model_baselineOLS

outreg2 using Tab1.doc, ctitle(Model 1 (OLS), GTME) ///
sortvar 	(  $IndepDist $IndepAidln 	$Main_controls $FE ) ///
keep 		(  $IndepDist $IndepAidln 	$Main_controls $FE	 ) ///
addstat 	(  $Add_stat_OLS ) ///
tex dec(3) pdec(3) ///
addtext(Cluster SE, 		YES) ///
replace adj label   

** 2sls
estimates restore  Model_baseline2SLS

outreg2 using Tab1.doc, ctitle(Model 1 (2SLS), GTME) ///
sortvar 	(  $IndepDist $IndepAidln 	$Main_controls $FE	 ) ///
keep 		(  $IndepDist $IndepAidln 	$Main_controls $FE	 ) ///
addstat 	(  $Add_stat ) ///
tex dec(3) pdec(3) ///
addtext(Cluster SE, 		YES) ///
append adj label  

* note: "-99999" to be replaced with "." manually in table


*---------------------------------------------------------------------*
*                    Table 2                  
*---------------------------------------------------------------------*

** set global stats 2sls
global Add_stat			                                   	///
Wald chi2,										-9999, ///
Prob > chi2,									-9999, ///
Hansen J statistic,  							e(j),		///
J statistic degrees of freedom, 				e(jdf),		///
p-value of Hansen J statistic, 					e(jp),		///
GMM distance test statistic of endogeneity,   	e(estat), 	///
p-value of GMM distance test statistic, 		e(estatp),	///
Wald test of exogeneity:  Prob > chi2, 			-9999, 			///
Number of ID,									e(N_clust)

** set global stats CFP
global Add_stat_CFP                             	///
Hansen J statistic,  							-9999,		///
J statistic degrees of freedom, 				-9999,		///
p-value of Hansen J statistic, 					-9999,		///
GMM distance test statistic of endogeneity,   	-9999, 	///
p-value of GMM distance test statistic, 		-9999,	///
Wald test of exogeneity:  Prob > chi2, 			e(p_exog), 			///
Number of ID,									e(N_clust)


* essential 2
estimates 	restore  Model_Essential
outreg2 using Tab2.doc, ctitle(Model 2(2SLS), GTME) ///
sortvar 	(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
keep 		(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
addstat 	(  $Add_stat ) ///
tex dec(3) pdec(3) ///
addtext(Cluster SE, 		YES) ///
replace adj label  
 
* situational 3
estimates 	restore  Model_Situational
outreg2 using Tab2.doc, ctitle(Model 3(2SLS), GTME) ///
sortvar 	(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
keep 		(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
addstat 	(  $Add_stat ) ///
tex dec(3) pdec(3) ///
addtext(Cluster SE, 		YES) ///
append adj label  

* operational 4
estimates 	restore  Model_Operational
outreg2 using Tab2.doc, ctitle(Model 4(2SLS), GTME) ///
sortvar 	(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
keep 		(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
addstat 	(  $Add_stat ) ///
tex dec(3) pdec(3) ///
addtext(Cluster SE, 		YES) ///
append adj label  

* strategic 5
estimates 	restore  Model_Strategic
outreg2 using Tab2.doc, ctitle(Model 5(2SLS), GTME) ///
sortvar 	(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
keep 		(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
addstat 	(  $Add_stat ) ///
tex dec(3) pdec(3) ///
addtext(Cluster SE, 		YES) ///
append adj label  

* ethnic 6
estimates 	restore  Model_Ethnic
outreg2 using Tab2.doc, ctitle(Model 6(2SLS), GTME) ///
sortvar 	(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
keep 		(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
addstat 	(  $Add_stat ) ///
tex dec(3) pdec(3) ///
addtext(Cluster SE, 		YES) ///
append adj label  

* additional 7
estimates 	restore  Model_Additional
outreg2 using Tab2.doc, ctitle(Model 7(2SLS), GTME) ///
sortvar 	(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
keep 		(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
addstat 	(  $Add_stat ) ///
tex dec(3) pdec(3) ///
addtext(Cluster SE, 		YES) ///
append adj label  

* NoOut 8
estimates 	restore  Model_NoOutAid
outreg2 using Tab2.doc, ctitle(Model 8(2SLS), GTME) ///
sortvar 	(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
keep 		(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
addstat 	(  $Add_stat ) ///
tex dec(3) pdec(3) ///
addtext(Cluster SE, 		YES) ///
append adj label  

* probit 9
estimates 	restore  Model_probit
outreg2 using Tab2.doc, ctitle(Model 9(CF), GTVic) ///
sortvar 	(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
keep 		(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
addstat 	(  $Add_stat_CFP ) ///
tex dec(3) pdec(3) ///
addtext(Cluster SE, 		YES) ///
append  label  

*tobit 10
estimates 	restore  Model_Tobit
outreg2 using Tab2.doc, ctitle(Model 10(2SLS), GTME) ///
sortvar 	(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
keep 		(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
addstat 	(  $Add_stat_CFP ) ///
tex dec(3) pdec(3) ///
addtext(Cluster SE, 		YES) ///
append  label  


* log ler 11
estimates 	restore  Model_Ln
outreg2 using Tab2.doc, ctitle(Model 11(2SLS), GTME) ///
sortvar 	(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
keep 		(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
addstat 	(  $Add_stat ) ///
tex dec(3) pdec(3) ///
addtext(Cluster SE, 		YES) ///
append adj label  

* log ler no out 12
estimates 	restore  Model_Ln_outl
outreg2 using Tab2.doc, ctitle(Model 12(2SLS), GTME) ///
sortvar 	(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
keep 		(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
addstat 	(  $Add_stat ) ///
tex dec(3) pdec(3) ///
addtext(Cluster SE, 		YES) ///
append adj label  

** alt av 13
estimates 	restore  Model_alt_dv
outreg2 using Tab2.doc, ctitle(Model 13(2SLS), GTME) ///
sortvar 	(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
keep 		(  $IndepDist $IndepAidln 	$Main_controls $Additional	 ) ///
addstat 	(  $Add_stat ) ///
tex dec(3) pdec(3) ///
addtext(Cluster SE, 		YES) ///
append adj label  


* note: "-99999" to be replaced with "." manually in table
* note: for Model 9 and Model 10 STATA creates additional columns for the 1st ste. these columns (9, 10, 19, 14) must be removed manually from the table in the dox file



